package wrs.hcs.functions;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Web_App {

	static
	Miscellaneous m = new Miscellaneous();
	
	public void step_1(WebDriver driver, String loc, String web_package){
		
		WebElement locationGC = driver.findElement(By.xpath("//input[@id='locGC']"));
		WebElement locationQC = driver.findElement(By.xpath("//input[@id='locQC']"));
		if(loc.equals("GC")){
			locationGC.click();
		}else{
			locationQC.click();
		}
		driver.findElement(By.xpath("//button[@id='submitForm']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		WebElement pair_package = driver.findElement(By.xpath("//input[@id='btnPairPack']"));
		WebElement single_package = driver.findElement(By.xpath("//input[@id='btnSinglePack']"));
		if(web_package.equals("Single")){
			single_package.click();
		}else{
			pair_package.click();
		}
		driver.findElement(By.xpath("//button[@id='btnConOK']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	
	public void step_2_single(WebDriver driver, String pin, String lastName){
		
		driver.findElement(By.xpath("//input[@name='hcsPin']")).sendKeys(pin);
		driver.findElement(By.xpath("//form[@id='WSForm']/div[2]/div[2]/input")).sendKeys(lastName);
		driver.findElement(By.xpath("//button[@class='btn']")).click();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//button[@id='btnPopUpAgree']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	
	public void step_2_pair(WebDriver driver, String pin1, String pin2, String lName1, String lName2){
		
		//Patient 1
		driver.findElement(By.xpath("//input[@id='pin1']")).sendKeys(pin1);
		driver.findElement(By.xpath("//input[@id='lastName1']")).sendKeys(lName1);
		driver.findElement(By.xpath("//form[@id='form1']/div[4]/button[2]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@id='errorPopupClose']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
						
		//Patient 2
		driver.findElement(By.xpath("//input[@id='pin2']")).sendKeys(pin2);
		driver.findElement(By.xpath("//input[@id='lastName2']")).sendKeys(lName2);
		driver.findElement(By.xpath("//form[@id='form2']/div[3]/button[2]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@id='errorPopupClose']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//button[@class='btn btn-primary']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//button[@id='submitForm']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	
	public void step_3_single(WebDriver driver, String select_package, String type, String room){
		int ss = 0;
		WebElement basic = driver.findElement(By.xpath("//a[@id='PACKBASC']"));
		WebElement cardi = driver.findElement(By.xpath("//a[@id='PACKCARDIO']"));
		WebElement diabe = driver.findElement(By.xpath("//a[@id='PACKDIAB']"));
		WebElement golde = driver.findElement(By.xpath("//a[@id='PACKGOLDEN']"));
		WebElement overw = driver.findElement(By.xpath("//a[@id='PACKOVRWEIGHT']"));
                
                if (select_package.equals("basic")){
//                     basic.click();
                     ss = 1;
                }
                if (select_package.equals("cardiometabolic")){
//                     cardi.click();
                     ss = 2;
                }
                if (select_package.equals("diabetes")){
//                     diabe.click();
                     ss = 3;
                }
                if (select_package.equals("golden")){
//                     golde.click();
                     ss = 4;
                }
                if (select_package.equals("overweight")){
//                     overw.click();
                     ss = 5;
                }

		switch (ss) {
                    case 1:
                            basic.click();
                        break;
                    case 2:
                            cardi.click();
                        break;
                    case 3:
                        diabe.click();
                        break;
                    case 4:
                        golde.click();
                        break;
                    case 5:
                        overw.click();
                        break;
                    default:
                        System.out.println("INVALID PACKAGE");
        }
		System.out.println(
				"Selected Package is: "+
				driver.findElement(By.xpath("//h4[@id='packageTitle']")).getText()
		);
		
		if(type.equals("inpatient")){
			driver.findElement(By.xpath("//input[@id='inPatient']")).click();
			if(room.equals("private")){
				driver.findElement(By.xpath("//input[@id='privateM_PackageRateNo']")).click();
				driver.findElement(By.xpath("//div[@id='privateM']/div[1]/a")).click();
				String popup_room = driver.findElement(By.xpath("//div[@id='description']")).getText();
				if(popup_room.equals("REGULAR PRIVATE")){
					System.out.println("Room displayed is CORRECT. " +popup_room);
				}else{
					System.out.println("Room displayed is INCORRECT. " +popup_room);
				}
			}else{
				driver.findElement(By.xpath("//input[@id='suiteM_PackageRateNo']")).click();
				driver.findElement(By.xpath("//div[@id='suiteM']/div[1]/a")).click();
				String popup_room = driver.findElement(By.xpath("//div[@id='description']")).getText();
				if(popup_room.equals("REGULAR SUITE")){
					System.out.println("Room displayed is CORRECT. " +popup_room);
				}else{
					System.out.println("Room displayed is INCORRECT. " +popup_room);
				}
			}
			driver.findElement(By.xpath("//button[@class='btn' and contains(.,'OK')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else{
			driver.findElement(By.xpath("//input[@id='outPatient']")).click();
			driver.findElement(By.xpath("//input[@id='opdM_PackageRateNo']")).click();
		}
		driver.findElement(By.xpath("//button[@id='submitForm']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	
	public void step_3_pair(WebDriver driver, String p1_package, String p2_package, String room){
		WebElement room_private = driver.findElement(By.xpath("//input[@id='PRIVATE']"));
		WebElement room_suite = driver.findElement(By.xpath("//input[@id='SUITE']"));
		if(room.equals("private")){
			room_private.click();
		}else{
			room_suite.click();
		}
		driver.findElement(By.xpath("//a[contains(.,'VIEW ROOM')]")).click();
		String popup_room = driver.findElement(By.xpath("//div[@id='description']")).getText();
		System.out.println(popup_room);
		
		String p1_pack = m.set_package_id(p1_package);
		String p2_pack = m.set_package_id(p2_package);
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		Actions actions = new Actions(driver);
        WebElement mouseover = driver.findElement(By.xpath("//tr[@id='"+p1_pack+"']/td[1]/input"));
        actions.moveToElement(mouseover);
        actions.click().perform();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//tr[@id='"+p2_pack+"']/td[2]/input")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//tr[@id='"+p1_pack+"']/td[1]/input")).click();
		driver.findElement(By.xpath("//button[@id='submitForm']")).click();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	}
	
	public void step_4_single(WebDriver driver, String email, String date, boolean guarantor){
		
		driver.findElement(By.xpath("//input[@id='emailAdd']")).sendKeys(email);
		driver.findElement(By.xpath("//input[@id='areaCode']")).sendKeys("44");
		driver.findElement(By.xpath("//input[@name='disabilityId']")).sendKeys("4325");
		Select time = new Select(driver.findElement(By.xpath("//select[@id='arrivalTime']")));
		time.selectByVisibleText("12:00 PM");
		if(guarantor==true){
			step_4_guarantor(driver, "");
		}
		driver.findElement(By.xpath("//input[@id='datepicker2']")).clear();
		driver.findElement(By.xpath("//input[@id='datepicker2']")).sendKeys(date);
		try{
			//driver.findElement(By.xpath("//label[contains(.,'Room Type of Choice')]")).click();
			driver.findElement(By.xpath("//p[@id='errorMessage']"));
			loop_date(driver, date, "single");
		}catch(Exception e){
			System.out.println("Date is available");
		}
		driver.findElement(By.xpath("//button[@id='btnContinue']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	
	public void loop_date(WebDriver driver, String date, String type){

		String new_date[] = date.split("/");
		int intDay= Integer.parseInt(new_date[1]);
		int intMonth= Integer.parseInt(new_date[0]);
		int intYear= Integer.parseInt(new_date[2]);
		intDay=intDay+1;
		if(intDay>30){
			intDay = 1;
			intMonth++;
		}
		if(intMonth>12){
			intDay=1;
			intMonth=1;
			intYear++;
		}
		String strDay = Integer.toString(intDay);
		String strMonth = Integer.toString(intMonth);
		String strYear = Integer.toString(intYear);
		String finalDate = strMonth+"/"+strDay+"/"+strYear;
		loop_input_date(driver, finalDate, type);
		
	}
	
	public void loop_input_date(WebDriver driver, String date, String type){
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		if(type.equals("pair")){
			driver.switchTo().frame(driver.findElement(By.id("patient1")));
		}
		driver.findElement(By.xpath("//input[@id='datepicker2']")).clear();
		driver.findElement(By.xpath("//input[@id='datepicker2']")).sendKeys(date);
		driver.findElement(By.xpath("//input[@name='disabilityId']")).click();
		if(type.equals("pair")){
			String mainWindowHandle = driver.getWindowHandles().iterator().next();
			driver.switchTo().window(mainWindowHandle);
		}
		System.out.println(date);
		try{
			if(!type.equals("pair")){
				driver.findElement(By.xpath("//p[@id='errorMessage']"));
			}
			driver.findElement(By.xpath("//input[@id='errorPopupClose']")).click();
			loop_date(driver, date, type);
		}catch(Exception e){
			System.out.println("New Date is available");
		}
	}
	
	public void step_4_pair(WebDriver driver, String email, String date, boolean guarantor){

		driver.switchTo().frame(driver.findElement(By.id("patient1")));
		driver.findElement(By.xpath("//input[@id='emailAdd']")).sendKeys(email);
		driver.findElement(By.xpath("//input[@id='areaCode']")).sendKeys("44");
		driver.findElement(By.xpath("//input[@id='phoneNum']")).sendKeys("999999999");
		Select time = new Select(driver.findElement(By.xpath("//select[@id='arrivalTime']")));
		time.selectByVisibleText("12:00 PM");
		driver.findElement(By.xpath("//input[@id='datepicker2']")).sendKeys(date);
		if(guarantor==true){
			step_4_guarantor(driver, "");
		}
		try{
			driver.findElement(By.xpath("//input[@name='disabilityId']")).click();
			String mainWindowHandle = driver.getWindowHandles().iterator().next();
			driver.switchTo().window(mainWindowHandle);
			driver.findElement(By.xpath("//input[@id='errorPopupClose']")).click();
			System.out.println(date);
			loop_date(driver, date, "pair");
		}catch(Exception e){
			System.out.println("Date is available");
		}
		String mainWindowHandle = driver.getWindowHandles().iterator().next();
		driver.switchTo().window(mainWindowHandle);
		for(int i=0; i<2; i++){
			driver.findElement(By.xpath("//div[@id='steps']")).sendKeys(Keys.ARROW_DOWN);}
		driver.findElement(By.xpath("//button[@class='btn btn-primary']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@id='errorPopupClose']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.switchTo().frame(driver.findElement(By.id("patient2")));
		driver.findElement(By.xpath("//input[@id='emailAdd']")).sendKeys(email);
		driver.findElement(By.xpath("//input[@id='areaCode']")).sendKeys("44");
		driver.findElement(By.xpath("//input[@id='phoneNum']")).sendKeys("999999999");
		driver.switchTo().window(mainWindowHandle);
		for(int i=0; i<2; i++){
			driver.findElement(By.xpath("//div[@id='steps']")).sendKeys(Keys.ARROW_DOWN);
		}
		driver.findElement(By.xpath("//button[@class='btn btn-primary']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	
	public void step_4_guarantor(WebDriver driver, String mainWindowHandle){
		if(!mainWindowHandle.isEmpty() || !mainWindowHandle.equals("")){
			driver.switchTo().window(mainWindowHandle);
		}
		driver.findElement(By.xpath("//input[@id='cardHolder']")).click();
		driver.findElement(By.xpath("//input[@id='notPat_lname']")).sendKeys("Guarantor");
		driver.findElement(By.xpath("//input[@id='notPat_fname']")).sendKeys("Single");
		driver.findElement(By.xpath("//input[@id='notPat_mname']")).sendKeys("Inpatient");
		Select relPatient = new Select(driver.findElement(By.xpath("//select[@id='not_relationTo']")));
		relPatient.selectByVisibleText("AUNT");
		
	}
	
	public void step_5(WebDriver driver){
		
		driver.findElement(By.xpath("//button[@id='dialog_link']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//button[@id='iagree']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	
	public void step_payment(WebDriver driver, String mode_payment){
		if(mode_payment.equals("visa")){
			driver.findElement(By.xpath("//img[@src='/res/svr/default/images/cards/card_sm_visa.gif']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//input[@id='CardNumber']")).sendKeys("4005550000000001");
		}else{
			driver.findElement(By.xpath("//img[@src='/res/svr/default/images/cards/card_sm_masterc.gif']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//input[@id='CardNumber']")).sendKeys("5123456789012346");
		}
		driver.findElement(By.xpath("//input[@id='CardMonth']")).sendKeys("05");
		driver.findElement(By.xpath("//input[@id='CardYear']")).sendKeys("17");
		driver.findElement(By.xpath("//input[@id='Securecode']")).sendKeys("210");
		driver.findElement(By.xpath("//input[@id='Paybutton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		WebDriverWait wait = new WebDriverWait(driver,60);
		wait.until(
		    ExpectedConditions.elementToBeClickable(
		        By.xpath("//button[@id='printPrev']")));
	}
	
	
}
